package com.jdbc;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;

import com.test.DataBaseUtil;

public class GetTableInfoUtil
{
    public static Map<String, String> getTableNameAndType()
    {
    	System.out.println("create table start");
        Map<String, String> map = new HashMap<String,String>();
        JSONObject jsonObject = null;
        JSONObject tableObj = null;
        JSONArray columnObJsonArray = null;
        Connection getConnection = ConnectionJDBC.jdbcConnection(); //DataBaseUtil.getConn();// 
        PreparedStatement pst = null;
        try
        {
            DatabaseMetaData dbmd = getConnection.getMetaData();
//            System.out.println(dbmd.getDatabaseProductName());
            ResultSet resultSet = dbmd.getTables(null, "%", "%", new String[] {"TABLE"});
            while (resultSet.next())
            {
                tableObj = new JSONObject();
                columnObJsonArray = new JSONArray();
                String tableName = resultSet.getString("TABLE_NAME");
//                System.out.println("����" + tableName + "\t\n���ֶ���Ϣ��");
                pst = getConnection.prepareStatement("select * from " + tableName);
                ResultSetMetaData rsd = pst.executeQuery().getMetaData();
                for (int i = 0; i < rsd.getColumnCount(); i++)
                {
                    jsonObject = new JSONObject();
                    jsonObject.put("javaType", rsd.getColumnClassName(i + 1));
                    jsonObject.put("dbType", rsd.getColumnTypeName(i + 1));
                    jsonObject.put("columnName", rsd.getColumnName(i + 1));
                    jsonObject.put("columnLen", rsd.getColumnDisplaySize(i + 1));
//                    System.out.print("  java���ͣ�" + rsd.getColumnClassName(i + 1));
//                    System.out.print("  ��ݿ�����:" + rsd.getColumnTypeName(i + 1));
//                    System.out.print("  �ֶ����:" + rsd.getColumnName(i + 1));
//                    System.out.print("  �ֶγ���:" + rsd.getColumnDisplaySize(i + 1));
//                    System.out.println();
                    columnObJsonArray.put(jsonObject);
                }
                tableObj.put(tableName, columnObJsonArray);
                map.put(tableName, tableObj.toString());
//                System.out.println("map �е�Key=" + tableName + "   clunm = " + tableObj.toString());
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if (getConnection != null)
                {
                    getConnection.close();
                    getConnection = null;
                }
                if (pst != null)
                {
                    pst.close();
                    pst = null;
                }
                
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
        System.out.println("create table end");
        return map;
    }
}
